// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package com.example.connect;

// snippet-start:[connect.java2.delete.main]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.connect.ConnectClient;
import software.amazon.awssdk.services.connect.model.ConnectException;
import software.amazon.awssdk.services.connect.model.DeleteInstanceRequest;

/**
 * Before running this Java V2 code example, set up your development
 * environment, including your credentials.
 *
 * For more information, see the following documentation topic:
 *
 * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
 */
public class DeleteInstance {
    public static void main(String[] args) {
        final String usage = """

                Usage:    <instanceId>

                Where:
                   instanceId - The id of the instance to delete.

                """;

        if (args.length != 1) {
            System.out.println(usage);
            System.exit(1);
        }

        String instanceId = args[0];
        Region region = Region.US_EAST_1;
        ConnectClient connectClient = ConnectClient.builder()
                .region(region)
                .build();

        deleteSpecificInstance(connectClient, instanceId);
    }

    public static void deleteSpecificInstance(ConnectClient connectClient, String instanceId) {
        try {
            DeleteInstanceRequest instanceRequest = DeleteInstanceRequest.builder()
                    .instanceId(instanceId)
                    .build();

            connectClient.deleteInstance(instanceRequest);
            System.out.println("Instance was successfully deleted.");

        } catch (ConnectException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }
}
// snippet-end:[connect.java2.delete.main]
